// pages/market/market.js
const app = getApp()
Page({

  data: {
    userID:'',
    show: false,
    duration: 300,
    position: 'right',
    round: false,
    overlay: true,
    customStyle: 'height: 30%;',
    overlayStyle: '',
    array:[],
    page:1,
    clickItem:{},
    purValue:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const that=this
    wx.request({
      url: `http://localhost:8080/trade/allcbalance?page=${this.data.page}`,
      header: {
        'content-type': 'application/json' // 默认值
      }, 
      success (res) {
        console.log(res.data)
        const array=res.data
        let page=2
        that.setData({array,page})
      }
    })

    wx.request({
      url: `http://localhost:8080/user/getMyMsg?WXID=${app.WXID}`,
      header: {
        'content-type': 'application/json' // 默认值
      }, 
      success (res) {
        console.log("user",res.data[0])
        let userId=res.data[0].id
        that.setData({userId})
      }
    })

    
  },

  upper(e) {
    console.log(e)
  },

  lower(e) {
    let page=this.data.page+1
    this.setData({page})
    wx.request({
      url: `http://localhost:8080/trade/allcbalance?page=${page}`,
      header: {
        'content-type': 'application/json' // 默认值
      }, 
      success (res) {
        console.log(res.data)
        const array=this.data.array.concat(res.data)
        
        that.setData({array})
      }
    })
    console.log(e)
  },

  scroll(e) {
    console.log(e)
  },

  scrollToTop() {
    this.setAction({
      scrollTop: 0
    })
  },
  popup(e) {
    const position = e.currentTarget.dataset.index
    let clickItem={balance:0,companyid:0,price:0}
    clickItem=e.currentTarget.dataset
    console.log(clickItem)
    let customStyle = ''
    let duration = this.data.duration
    this.setData({
      position,
      show: true,
      customStyle,
      duration,
      clickItem
    })
  },
  exit() {
    this.setData({show: false})
    // wx.navigateBack()
  },

  goTo(e) {
    wx.navigateTo({url: `../shareElement/index`})
  },

  onBeforeEnter(res) {
    // console.log(res)
  },
  onEnter(res) {
    // console.log(res)
  },
  onAfterEnter(res) {
    // console.log(res)
  },
  onBeforeLeave(res) {
    // console.log(res)
  },
  onLeave(res) {
    // console.log(res)
  },
  onAfterLeave(res) {
    // console.log(res)
  },

  onClickOverlay(res) {
    // console.log(res)
  },

  handleChange ({ detail }) {
    this.setData({
      purValue: detail.value
    })
  },
  purchase(){
    const that=this
    wx.request({
      url: `http://localhost:8080/market/add_order`,
      method:'POST',
      data:{
        purchaseId:that.data.userId,
        sellID:that.data.clickItem.companyId,
        quantity:that.data.purValue,
        price:that.data.purValue*that.data.clickItem.balance/that.data.clickItem.price
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      }, 
      success (res) {
        console.log(res.data)
        const array=res.data
        let page=2
        that.setData({array,page})
      }
    })

    wx.redirectTo({
      url: './puechase-result/index'
    })
  }
})